import 'package:flutter/material.dart';
import 'package:flutter_component_pay/flutter_component_pay.dart';
import 'package:flutter_component_pay/src/theme.dart';
import 'package:flutter_component_pay/src/widgets/abs_third_pay_view.dart';

/* 
 * 目前提供三中支付方式（具体支付类型需要根据服务器返回）
 */
enum AbsPayWay {
  wxPay,
  aliPay,
  union,
}

/* 
 * 支付弹窗
 */
class AbsPaySheet {
  /// sheet背景颜色
  late Color sheetBackgroudColor;

  /// 支付标题
  late String title;

  /// 标题样式
  late TextStyle titleStyle;

  /// 副标题
  late String subTitle;

  /// 副标题样式
  late TextStyle subTitleStyle;

  /// 金额颜色
  late Color priceColor;

  /// sheet 圆角
  late double borderRadius;

  /// 点击背景是否消失. true:点击背景消失
  late bool isDismissible;

  /// 遮罩背景颜色
  late Color barrierColor;

  /// 支付标题style
  late TextStyle payTitleStyle;

  /// 总金额小数点后保留位数(默认保留小数点2位)
  late int asFixed;

  /// 默认选择的支付（默认微信支付）
  late AbsPayWay defaultPayWay;

  /// 底部容器视图的高度
  late double bottomWidgetHeight;

  /// 底部按钮高度
  late double bottomButtonHeight;

  /// 底部左侧按钮标题
  late String bottomLeftTitle;

  /// 底部右侧按钮标题
  late String bottomRightTitle;

  /// 底部左侧按钮标题样式
  late TextStyle bottomLeftTitleStyle;

  /// 底部右侧按钮标题样式
  late TextStyle bottomRightTitleStyle;

  /// 底部左侧按钮装饰
  late BoxDecoration bottomLeftDecoration;

  /// 底部右侧按钮装饰
  late BoxDecoration bottomRightDecoration;

  /// 支付选中图片
  Image? checkedImage;

  /// 支付未选中图片
  Image? uncheckedImage;

  /// 部件高度
  double? height;

  /* 
   * 获取 AbsPaySheet 实例
   */
  static AbsPaySheet get instance => _instance;

  /* 
   * 单例
   */
  factory AbsPaySheet() => _instance;
  static final AbsPaySheet _instance = AbsPaySheet._internal();

  AbsPaySheet._internal() {
    /// set deafult value
    sheetBackgroudColor = const Color(0xfff3f3f3);
    title = '支付订单';
    titleStyle = const TextStyle(
      color: Colors.black,
      fontSize: 26,
      fontWeight: FontWeight.w900,
    );
    subTitle = '合计支付';
    subTitleStyle = const TextStyle(
      color: Colors.black,
      fontSize: 16,
      fontWeight: FontWeight.w500,
    );
    priceColor = const Color(0xfff56200);
    borderRadius = 16;
    isDismissible = false;
    barrierColor = const Color(0x99000000);
    asFixed = 2;
    payTitleStyle = const TextStyle(
      color: Colors.black,
      fontSize: 16,
      fontWeight: FontWeight.bold,
    );
    defaultPayWay = AbsPayWay.wxPay;
    bottomWidgetHeight = 88;
    bottomButtonHeight = 40;
    bottomLeftTitle = '取消';
    bottomRightTitle = '确认支付';
    bottomLeftTitleStyle = const TextStyle(
      color: Color(0xfff56200),
      fontSize: 16,
      fontWeight: FontWeight.w500,
    );
    bottomRightTitleStyle = const TextStyle(
      color: Colors.white,
      fontSize: 16,
      fontWeight: FontWeight.bold,
    );
    bottomLeftDecoration = BoxDecoration(
      color: Colors.white,
      borderRadius: BorderRadius.circular(bottomButtonHeight * 0.5),
      border: Border.all(color: const Color(0xffffb219), width: 0.5),
    );
    bottomRightDecoration = BoxDecoration(
      borderRadius: BorderRadius.circular(bottomButtonHeight * 0.5),
      gradient: const LinearGradient(
        colors: [Color(0xffff9900), Color(0xffffc843)],
        stops: [0, 1],
        begin: Alignment.bottomCenter,
        end: Alignment.centerLeft,
      ),
    );
  }

  /// 显示支付sheet
  static void showPay(
    double price, {
    required BuildContext context,
    required List<AbsThirdChannel> payChannels,
    ValueChanged<AbsThirdChannel>? onPay,
    VoidCallback? onClose,
    ValueChanged<AbsThirdChannel?>? onBottomLeftEvent,
    ValueChanged<AbsThirdChannel?>? onBottomRightEvent,
  }) {
    _instance._show(
      price,
      context: context,
      payChannels: payChannels,
      onPay: onPay,
      onClose: onClose,
      onBottomLeftEvent: onBottomLeftEvent,
      onBottomRightEvent: onBottomRightEvent,
    );
  }

  /// internal handler show
  void _show(
    double price, {
    required BuildContext context,
    required List<AbsThirdChannel> payChannels,
    ValueChanged<AbsThirdChannel>? onPay,
    VoidCallback? onClose,
    ValueChanged<AbsThirdChannel?>? onBottomLeftEvent,
    ValueChanged<AbsThirdChannel?>? onBottomRightEvent,
  }) {
    MediaQueryData queryData = MediaQueryData.fromView(View.of(context));

    showModalBottomSheet(
      context: context,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(
          top: Radius.circular(AbsPayTheme.borderRadius),
        ),
      ),
      clipBehavior: Clip.antiAlias,
      enableDrag: false,
      isScrollControlled: true,
      isDismissible: AbsPayTheme.isDismissible,
      barrierColor: AbsPayTheme.barrierColor,
      constraints: BoxConstraints(
        maxWidth: queryData.size.width,
        maxHeight: AbsPayTheme.height ?? queryData.size.height * 0.75,
      ),
      builder: (context) => AbsThirdPayView(
        price: price,
        payChannels: payChannels,
        onPay: onPay,
        onClose: onClose,
        onBottomLeftEvent: onBottomLeftEvent,
        onBottomRightEvent: onBottomRightEvent,
      ),
    );
  }
}
